<template>
  <div>
    <div class="picker-control__bar stock-bucket__bar">
      <div class="picker-control__bar__cancel" @click="cancel">取消</div>
      <div class="picker-control__bar__tit">{{tit}}</div>
      <div class="picker-control__bar__confirm" @click="confirm">确认</div>
    </div>
    <div class="stock-bucket__container">
      <div class="stock-bucket__tip">
        注：可以选择已购买过的水桶进行兑换抵消<br>1、线下配送员会逐一核实确认，请依据实际填写<br>2、空桶兑换以原价进行兑换，换桶以70%桶价进行兑换<br>3、换桶和空桶总数不能超过总购买的水桶数量</div>
      <van-radio-group class="stock-bucket__type" v-model="exchangeBucketType" @change="exchangeBucketChange">
        <div class="stock-bucket__type__item">
          <van-radio :name="0">
            欠桶抵押
            <div class="stock-bucket__count">{{exchangeBucketType == 0 ? countBucketNum : 0}}</div>
          </van-radio>
        </div>
        <div class="stock-bucket__type__item">
          <van-radio :name="1">
            押桶购买
            <div class="stock-bucket__count">{{exchangeBucketType == 1 ? countBucketNum : 0}}</div>
          </van-radio>
        </div>
      </van-radio-group>
      <div class="stock-bucket__item" v-for="(item, index) in list">
        <!-- <div class="stock-bucket__item__check">
          <van-checkbox v-model="item.checked"></van-checkbox>
        </div> -->
        <div class="stock-bucket__item__img"></div>
        <div>
          <div class="stock-bucket__item__info">
            <div class="stock-bucket__item__info__name">{{item.bucketName}}</div>
            <div class="stock-bucket__item__info__price">
              购桶数：{{item.count}}
            </div>
            <div class="stock-bucket__item__info__price">
              桶价：{{item.bucketPrice}}元
            </div>
          </div>
          <div class="stock-bucket__item__control">
            <div>空桶：</div>
            <van-stepper v-model="item.emptyChange" :min="0" :max="item.emptyChangeMax" input-width="25px" />
            &nbsp;
            <div>换桶：</div>
            <van-stepper v-model="item.bucketChange" :min="0" :max="item.bucketChangeMax" input-width="25px" />
          </div>
          <div class="stock-bucket__item__total">
            已选{{item.checkedCount}}桶
          </div>
        </div>

      </div>
    </div>

  </div>
</template>

<script>
  export default {
    props: {
      bucketList: {
        type: Array,
        default: []
      },
      tit: {
        type: String,
        default: '押桶信息'
      }
    },
    data() {
      return {
        list: [],
        exchangeBucketType: 0
      }
    },
    watch: {
      bucketList(val) {
        let list = [];
        val.forEach(el => {
          let obj = JSON.parse(JSON.stringify(el));
          obj.checked = false;
          obj.max = el.count;
          obj.emptyChange = 0; //空桶兑换数量
          obj.emptyChangeMax = el.count; //空桶兑换上限
          obj.bucketChange = 0; //换桶兑换数量
          obj.bucketChangeMax = el.count; //换桶兑换上限
          obj.checkedCount = 0;
          list.push(obj);
        });
        this.list = list;
      },
      list: {
        //监听更新水桶兑换上限
        handler(val) {
          val.forEach(el => {
            el.emptyChangeMax = el.count - el.bucketChange;
            el.bucketChangeMax = el.count - el.emptyChange;
            el.checkedCount = el.emptyChange + el.bucketChange
          });
        },
        deep: true
      }
    },
    computed: {
      //计算押桶数量
      countBucketNum(){
        let count = 0;
        this.list.forEach(el => {
          count += el.count - (el.emptyChange + el.bucketChange);
        });
        return count;
      }
    },
    methods: {
      chooseBucket(item) {
        item.checked = !item.checked;
      },
      cancel() {
        this.$emit('cancel');
      },
      confirm() {
        //选择兑桶信息以及支付方式
        this.$emit('confirm', this.list, this.exchangeBucketType);
      },
      //更换押桶方式后清空
      exchangeBucketChange() {
        this.list.forEach(el => {
          el.emptyChange = 0;
          el.bucketChange = 0;
        });
      }
    },
  }

</script>

<style lang="less" scoped>
  .stock-bucket__bar {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    box-sizing: border-box;
  }

  .stock-bucket__container {
    position: absolute;
    top: 40px;
    bottom: 0;
    width: 100%;
    overflow: auto;
  }

  .stock-bucket__item {
    display: flex;
    padding: 10px;
    border-bottom: 1px solid #f5f5f5;
    align-items: center;

    .stock-bucket__item__img {
      flex: 0 0 40px;
      height: 40px;
      margin: 0 10px;
      background-image: url('../assets/product.png');
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
    }

    .stock-bucket__item__info {
      display: flex;
      flex: 1;
      align-items: center;
      justify-content: space-between;

      .stock-bucket__item__info__name {
        font-size: 14px;
      }

      .stock-bucket__item__info__price {
        font-size: 10px;
        color: #999;
      }
    }

    .stock-bucket__item__control {
      display: flex;
      align-items: center;
      margin-top: 5px;
      font-size: 12px;
    }

    .stock-bucket__item__total {
      margin-top: 3px;
      text-align: right;
      font-size: 10px;
      color: #999;
    }
  }

  .stock-bucket__type {
    display: flex;
    justify-content: space-around;
    font-size: 12px;

    .stock-bucket__type__item {
      position: relative;
      .stock-bucket__count {
        display: inline-block;
        padding: 0 4px;
        height: 16px;
        line-height: 16px;
        font-size: 10px;
        border-radius: 10px;
        text-align: center;
        background: #1989fa;
        color: #fff;
      }
    }

  }

  .stock-bucket__tip {
    color: #999;
    font-size: 10px;
    padding: 10px 10px 5px 10px;
  }

</style>
